const mongoose = require('mongoose');

/**
 * 手相分析数据模型
 * 定义手相分析结果的数据库结构
 */

// 手相主要线条特征 Schema
const PalmLineSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    description: '线条名称（如生命线、智慧线、感情线等）'
  },
  description: {
    type: String,
    required: true,
    description: '线条特征描述'
  },
  quality: {
    type: String,
    enum: ['清晰', '模糊', '中断', '弯曲', '深刻'],
    description: '线条质量特征'
  }
}, { _id: false });

// 手掌形状特征 Schema
const PalmShapeSchema = new mongoose.Schema({
  type: {
    type: String,
    required: true,
    enum: ['方形手', '长形手', '圆形手', '尖形手'],
    description: '手掌形状类型'
  },
  description: {
    type: String,
    required: true,
    description: '形状特征描述'
  }
}, { _id: false });

// 性格特质 Schema
const PersonalityTraitSchema = new mongoose.Schema({
  trait: {
    type: String,
    required: true,
    description: '性格特质名称'
  },
  description: {
    type: String,
    required: true,
    description: '性格特质详细描述'
  },
  strength: {
    type: Number,
    min: 1,
    max: 10,
    description: '特质强度（1-10）'
  }
}, { _id: false });

// 手相分析主 Schema
const PalmAnalysisSchema = new mongoose.Schema({
  // 分析ID
  id: {
    type: String,
    required: true,
    unique: true,
    description: '分析唯一标识符'
  },
  
  // 用户ID（关联用户）
  userId: {
    type: String,
    required: true,
    index: true,
    description: '关联的用户ID'
  },
  
  // 分析图片URL
  imageUrl: {
    type: String,
    required: true,
    description: '上传的手部图片URL'
  },
  
  // 手部类型（左手/右手）
  handType: {
    type: String,
    required: true,
    enum: ['left', 'right'],
    description: '分析的手部类型'
  },
  
  // 手掌形状特征
  palmShape: {
    type: PalmShapeSchema,
    required: true,
    description: '手掌形状分析结果'
  },
  
  // 主要线条特征
  majorLines: {
    type: [PalmLineSchema],
    required: true,
    description: '主要手相线条分析'
  },
  
  // 性格特质分析
  personalityTraits: {
    type: [PersonalityTraitSchema],
    required: true,
    description: '基于手相的性格特质分析'
  },
  
  // 综合分析描述
  analysisSummary: {
    type: String,
    required: true,
    description: '手相综合分析描述'
  },
  
  // 专业建议
  professionalAdvice: {
    type: String,
    description: '针对职业发展的建议'
  },
  
  // 感情建议
  relationshipAdvice: {
    type: String,
    description: '针对感情关系的建议'
  },
  
  // 健康建议
  healthAdvice: {
    type: String,
    description: '针对健康的建议'
  },
  
  // 财运分析
  wealthAnalysis: {
    type: String,
    description: '财运分析'
  },
  
  // 创建时间
  createdAt: {
    type: Date,
    default: Date.now,
    description: '分析创建时间'
  },
  
  // 更新时间
  updatedAt: {
    type: Date,
    default: Date.now,
    description: '分析更新时间'
  }
});

// 在保存前更新updatedAt字段
PalmAnalysisSchema.pre('save', function(next) {
  this.updatedAt = Date.now();
  next();
});

// 索引优化
PalmAnalysisSchema.index({ userId: 1, createdAt: -1 });

module.exports = mongoose.model('PalmAnalysis', PalmAnalysisSchema);